package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;

import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActQuestionService;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActQuestionMapper;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActQuestion;
import java.util.stream.Stream;
@Service
public class ActQuestionServiceImpl implements IActQuestionService{
 	@Autowired
	private ActQuestionMapper actQuestionMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createActQuestion(ActQuestion actQuestion) {
		ZBeanUtil.setCreateInfo(actQuestion);
		ZBeanUtil.validateBean(actQuestion);
		actQuestionMapper.insertSelective(actQuestion);
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActQuestion> loadActQuestionById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actQuestionMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateActQuestion(ActQuestion actQuestion) {
		ZBeanUtil.setUpdatedInfo(actQuestion);
		ZBeanUtil.validateBean(actQuestion);
		actQuestionMapper.updateByPrimaryKeySelective(actQuestion);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteActQuestion(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		actQuestionMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setActQuestionStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		actQuestionMapper.updateStatus( id,  status) ;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActQuestionDataSet(Map<String, Object> params) {
		
		ParamMap pm=ParamMap.filterParam(params);
		pm.getStr("keyword").ifPresent(val->pm.updateParam("keyword", "%"+val+"%"));
		pm.getStr("reference").ifPresent(val->pm.updateParam("reference", "%"+val+"%"));
		pm.getStr("reference").ifPresent(val->pm.updateParam("reference", "%"+val+"%"));
		pm.getStr("tags").ifPresent(val->pm.updateParam("tags", "%"+val+"%"));
		pm.getStr("content").ifPresent(val->pm.updateParam("content", "%"+val+"%"));

		DataSet ds=DataSet.newDS2(actQuestionMapper.selectListCount(pm), actQuestionMapper.selectMapListPage(pm));
		return ds;
	}
	

	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ActQuestion> loadActQuestionsByIds(List<Long> questIds){
		if(ZObjectUtil.isEmpty(questIds)) {
			return	actQuestionMapper.selectByIds(questIds);
		}
		return Lists.newArrayList();
	}
	
}